﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UrlChecker.Setting
{
    public partial class SiteMange : QT.Windows.Shell.GlassForm
    {
        public SiteMange()
        {
            InitializeComponent();
            loadData();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = AeroColor;
        }


        #region Method

        private void loadData()
        {
            DataTable dt = QT.SEO.Site.getAll();
            this.dgvList.AutoGenerateColumns = false;
            this.dgvList.Columns[0].DataPropertyName = "sitename";
            this.dgvList.Columns[1].DataPropertyName = "sitedomain";
            this.dgvList.Columns[2].DataPropertyName = "guid";
            this.dgvList.AutoResizeColumns();
            this.dgvList.DataSource = dt;
        }

        #endregion

        #region Event

        private void dgvList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("共{0}条记录", this.dgvList.RowCount);
        }

        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.tbSiteName.Text))
            {
                MessageBox.Show("请输入站点名称");
                return;
            }
            if (string.IsNullOrEmpty(this.tbSiteDomain.Text))
            {
                MessageBox.Show("请输入站点域名");
                return;
            }

            int rval = QT.SEO.Site.Add(this.tbSiteName.Text, this.tbSiteDomain.Text);
            switch (rval)
            {
                case -2:
                    MessageBox.Show("站点名称已存在");
                    break;
                case -1:
                case 0:
                    MessageBox.Show("保存失败");
                    break;
                case 1:
                    MessageBox.Show("保存成功");
                    loadData();
                    break;
                default:
                    MessageBox.Show("内部错误，错误代码："+rval);
                    break;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> guids = new List<string>();
            foreach (DataGridViewRow row in this.dgvList.SelectedRows) guids.Add(row.Cells[2].Value.ToString());

            int rval = QT.SEO.Site.Delete(guids);
            if (rval < 1) MessageBox.Show("操作失败");
            else loadData();
        }
    }
}
